var HotBook = function(id, pic, name, price){
    this.BookId = id;
	this.Pic = pic;
	this.Name= name;
	this.Price = price;
}

var ListHotBook = function(hotBooks){
	this.HotBooks = hotBooks;
}

function createHotBooks(hotBooks){
	   var arrHotBooks = [];
       var result = [];
       var hotBook = null;
	
       arrHotBooks = hotBooks.getElementsByTagName("book");
       try{
       	for(var i = 0; i < arrHotBooks.length; i++){
           hotBook = new HotBook(arrHotBooks[i].getElementsByTagName("bookId")[0].childNodes[0].nodeValue, arrHotBooks[i].getElementsByTagName("picture")[0].childNodes[0].nodeValue, arrHotBooks[i].getElementsByTagName("bookName")[0].childNodes[0].nodeValue, arrHotBooks[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
           result.push(hotBook);
       	}
       } catch(ex){}
       return result;
}


function createHotBooksNode(arrHotBooks){
	    var key = "home";
		addActive(key);
		var rootNodeHotBooks = $id("itemsHotBook");
		var hotBooks = null;
		var nodeItem = null;
		var nodeImg = null;
		var nodeItembot = null;
		var nodeName = null;
		var nodePrice = null;
		var lengthListHotBook = arrHotBooks.length;

		// Delete child node
		rootNodeHotBooks.innerHTML = "";

		//Set node Hotbooks
		for (var i = 0; i < lengthListHotBook; i++) {
			hotBooks = arrHotBooks[i];

			nodeItem = createNode("a");
			nodeItem.addClass("item");
			nodeItem.setAttribute("href", "#");
            nodeItem.setAttribute("id", hotBooks.BookId);
			nodeItem.addEventListener("click", getProductDetail,false); 

			nodeImg = createNode("img");
			nodeImg.addClass("item-img");
			nodeImg.src=hotBooks.Pic;

			nodeItembot = createDiv();
			nodeItembot.addClass("item-bot");

			nodeName = createDiv();
			nodeName.addClass("item-name");
			nodeName.setText(hotBooks.Name);

			nodePrice = createDiv();
			nodePrice.addClass("item-price");
			nodePrice.setText(hotBooks.Price);

			rootNodeHotBooks.appendChild(nodeItem);

			nodeItem.appendChild(nodeImg);
			nodeItem.appendChild(nodeItembot);

			nodeItembot.appendChild(nodeName);
			nodeItembot.appendChild(nodePrice);
		}
}

function callServiceHotBook(){
	        ajax.get({
	        url: "http://localhost:8080/book-service/public/book/store/hotbook",

	        success: function(data){
	           var hotBooks = createHotBooks(data);
	           createHotBooksNode(hotBooks);
	        },

	        failure: function(){
	            console.log("fail callServiceHotBook");
	        }
        });
}




